package myapps.ttclient.activity;

import myapps.ttclient.R;
import myapps.ttclient.activity.connection.ConnectionEditActivity;
import myapps.ttclient.activity.connection.ConnectionEditActivity_Bluetooth;
import myapps.ttclient.activity.connection.ConnectionEditActivity_Wifi;
import myapps.ttclient.adapter.ConnectionListAdapter_Bluetooth;
import myapps.ttclient.adapter.ConnectionListAdapter_Wifi;
import myapps.ttclient.app.TTClientApplication;
import myapps.ttclient.connection.Connection;
import myapps.ttclient.connection.ConnectionListManager;
import myapps.ttclient.connection.Connection_Bluetooth;
import myapps.ttclient.connection.Connection_Wifi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class ConnectionSettingsActivity extends Activity 
implements OnItemClickListener, OnItemLongClickListener, android.content.DialogInterface.OnClickListener {

	private final static int REQUEST_CODE_WIFI_CNET_SETTING = 10001;
	private final static int REQUEST_CODE_BLUETOOTH_CNET_SETTING = 10002;

	private TTClientApplication application;

	private SharedPreferences preferences;

	private ConnectionListManager connectionListManager;

	private ConnectionListAdapter_Wifi wifiConnectionListAdapter;

	private ConnectionListAdapter_Bluetooth bluetoothConnectionListAdapter;

	private ListView lvWifiConnections;

	private ListView lvBluetoothConnections;

	private TabHost tabhost;

	private AlertDialog alertDialogItemLongClick;

	private int itemLongClickPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.application = (TTClientApplication) this.getApplication();

		this.preferences = this.application.getPreferences();

		this.connectionListManager = this.application.getConnectionListManager();

		this.wifiConnectionListAdapter = new ConnectionListAdapter_Wifi(
				getApplicationContext(), connectionListManager);

		this.bluetoothConnectionListAdapter = new ConnectionListAdapter_Bluetooth(
				getApplicationContext(), connectionListManager);

		/////////////////////////////////////////////////////////////////////////////////////

		setContentView(R.layout.connection_settings);

		tabhost = (TabHost) findViewById(R.id.tabhost);
		tabhost.setup();

		TabHost.TabSpec spec;

		spec = tabhost.newTabSpec("tab1");
		spec.setContent(R.id.tab1);
		spec.setIndicator("Wifi", getResources().getDrawable(R.drawable.ic_tab_wifi));
		tabhost.addTab(spec);

		spec = tabhost.newTabSpec("tab2");
		spec.setContent(R.id.tab2);
		spec.setIndicator("Bluetooth", getResources().getDrawable(R.drawable.ic_tab_bluetooth));
		tabhost.addTab(spec);

		spec = tabhost.newTabSpec("tab3");
		spec.setContent(R.id.tab3);
		spec.setIndicator("Hint", getResources().getDrawable(R.drawable.ic_tab_hint));
		tabhost.addTab(spec);

		tabhost.setCurrentTab(0);

		////////////////////////////////////////////////////////////////////////////////

		if(connectionListManager.getUsed() instanceof Connection_Wifi) {
			tabhost.setCurrentTab(0);
		} else if(connectionListManager.getUsed() instanceof Connection_Bluetooth){
			tabhost.setCurrentTab(1);
		}

		lvWifiConnections = (ListView) findViewById(R.id.lvWifiConnections);
		lvWifiConnections.setAdapter(wifiConnectionListAdapter);
		lvWifiConnections.setOnItemClickListener(this);
		lvWifiConnections.setOnItemLongClickListener(this);

		lvBluetoothConnections = (ListView) findViewById(R.id.lvBluetoothConnections);
		lvBluetoothConnections.setAdapter(bluetoothConnectionListAdapter);
		lvBluetoothConnections.setOnItemClickListener(this);
		lvBluetoothConnections.setOnItemLongClickListener(this);

		initAlertDialogItemClick();
	}

	private void initAlertDialogItemClick() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setItems(R.array.connection_action_name, this);
		this.alertDialogItemLongClick = builder.create();
	}

	@Override
	protected void onPause() {
		connectionListManager.Save();
		super.onPause();
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return true;
	}

	private void menu(int which, int position) {
		switch (which) {
		case 0:
			this.useConnection(position);
			break;
			
		case 1:
			this.unchoose(position);
			break;
			
		case 2:
			this.editConnection(position);
			break;
			
		case 3:
			this.removeConnection(position);
			break;
			
		case 4:
			break;
		}
	}

	private void useConnection(int position) {
		if(tabhost.getCurrentTab() == 0) {
			this.connectionListManager.useWifiConnectionList(position);
		} else if (tabhost.getCurrentTab() == 1) {
			this.connectionListManager.useBluetoothConnectionList(position);
		}

		this.refresh();
	}

	private void unchoose(int position) {
		if(tabhost.getCurrentTab() == 0) {
			if(this.connectionListManager
					.getConnectionList_Wifi().getUsedPosition() == position) {
				this.connectionListManager.useWifiConnectionList(-1);
			} else {
				this.application.showToast("Invalid operation");
			}
		} else if (tabhost.getCurrentTab() == 1) {
			if(this.connectionListManager
					.getConnectionList_Bluetooth().getUsedPosition() == position) {
				this.connectionListManager.useBluetoothConnectionList(-1);
			} else {
				this.application.showToast("Invalid operation");
			}
		}

		this.refresh();
	}

	private void editConnection(int position) {
		Connection connection = null;

		if(tabhost.getCurrentTab() == 0) {
			connection = 
				this.connectionListManager.getConnectionList_Wifi()
				.get(this.itemLongClickPosition);
		} else if (tabhost.getCurrentTab() == 1) {
			connection = null;
			this.connectionListManager.getConnectionList_Bluetooth()
			.get(this.itemLongClickPosition);
		} 
		
		if(connection != null) {
			connection.edit(this);
			this.refresh();
		}
	}

	private void removeConnection(int position) {
		if(tabhost.getCurrentTab() == 0) {
			this.connectionListManager.getConnectionList_Wifi()
			.remove(this.itemLongClickPosition);
		} else if (tabhost.getCurrentTab() == 1) {
			this.connectionListManager.getConnectionList_Bluetooth()
			.remove(this.itemLongClickPosition);
		}

		this.refresh();
	}

	private void refresh() {
		//this.connectionListManager.getConnectionList_Wifi().sort();
		this.wifiConnectionListAdapter.notifyDataSetChanged();

		//this.connectionListManager.getConnectionList_Bluetooth().sort();
		this.bluetoothConnectionListAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {

		if(keyCode == KeyEvent.KEYCODE_MENU) {
			if(tabhost.getCurrentTab() == 0) {
				showNewWifiConnetionComfirmingDialog();
				
			} else if (tabhost.getCurrentTab() == 1) {
				showNewBluetoothConnetionComfirmingDialog();
			
			}
		}

		//return true;
		return super.onKeyUp(keyCode, event);
	}

	public void showNewWifiConnetionComfirmingDialog() {
		AlertDialog newWifiConnectionDialogBox = new AlertDialog.Builder(this)
		.setTitle("Question")
		.setMessage("Do you want to create a new wifi connection setting?")
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				createNewWifi();
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {

			}
		})
		.create();

		newWifiConnectionDialogBox.show();
	}

	private void createNewWifi() {
		Connection_Wifi c = new Connection_Wifi();

		ConnectionEditActivity.connectionParam = c;

		Intent intent = new Intent(ConnectionSettingsActivity.this, 
				ConnectionEditActivity_Wifi.class);

		intent.putExtra("type", "new");

		ConnectionSettingsActivity.this.startActivityForResult(
				intent, REQUEST_CODE_WIFI_CNET_SETTING);
	}

	private void createNewBluetooth() {
		Connection_Bluetooth c = new Connection_Bluetooth();

		ConnectionEditActivity.connectionParam = c;

		Intent intent = new Intent(ConnectionSettingsActivity.this, 
				ConnectionEditActivity_Bluetooth.class);

		intent.putExtra("type", "new");

		ConnectionSettingsActivity.this.startActivityForResult(
				intent, REQUEST_CODE_BLUETOOTH_CNET_SETTING);
	}

	public void showNewBluetoothConnetionComfirmingDialog() {
		AlertDialog newBluetoothConnectionDialogBox = new AlertDialog.Builder(this)
		.setTitle("Question")
		.setMessage("Do you want to create a new bluetooth connection setting?")
		.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				createNewBluetooth();
			}
		})
		.setNegativeButton("No", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {

			}
		})
		.create();

		newBluetoothConnectionDialogBox.show();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case REQUEST_CODE_WIFI_CNET_SETTING:
			if(resultCode == Activity.RESULT_OK) {
				//connectionListManager.getConnectionList_Wifi().sort();
				wifiConnectionListAdapter.notifyDataSetChanged();
			}
			break;

		case REQUEST_CODE_BLUETOOTH_CNET_SETTING:
			if(resultCode == Activity.RESULT_OK) {
				//connectionListManager.getConnectionList_Bluetooth().sort();
				bluetoothConnectionListAdapter.notifyDataSetChanged();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		this.itemLongClickPosition = position;
		this.alertDialogItemLongClick.show();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		if (dialog == this.alertDialogItemLongClick) {
			this.menu(which, itemLongClickPosition);
		}
	}
}
